package com.any.hook.core.webapi.mapper;


import com.any.hook.core.webapi.entity.DBInfo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * create table tb_dbinfo (name varchar(128) primary key,ip varchar(64),port varchar(64),driver_url varchar(128),db_user varchar(64),
 * db_password varchar(64),dbname varchar(128),driver_name varchar(256),params varchar(256),db_desc varchar(256));
 */

@Repository
@Mapper
public interface DbInfoMapper {
    @Insert("insert into tb_dbinfo(name,ip,port,driver_url,db_user,db_password,dbname,driver_name,params,db_desc) values(" +
            "#{dbinfo.name},#{dbinfo.ip},#{dbinfo.port},#{dbinfo.driverUrl},#{dbinfo.user},#{dbinfo.password},#{dbinfo.dbname}," +
            "#{dbinfo.driverName,jdbcType=VARCHAR},#{dbinfo.params,jdbcType=VARCHAR},#{dbinfo.desc,jdbcType=VARCHAR})")
    int insertInfo(@Param("dbinfo") DBInfo dbInfo);

    @Update("update tb_dbinfo set ip=#{dbinfo.ip}," +
            "port=#{dbinfo.port}," +
            "driver_url=#{dbinfo.driverUrl}," +
            "db_user=#{dbinfo.user}," +
            "db_password=#{dbinfo.password}," +
            "dbname=#{dbinfo.dbname}," +
            "driver_name=#{dbinfo.driverName}," +
            "params=#{dbinfo.params,jdbcType=VARCHAR}," +
            "db_desc=#{dbinfo.desc,jdbcType=VARCHAR}" +
            "where name=#{dbinfo.name}")
    int updateInfo(@Param("dbinfo") DBInfo dbInfo);

    @Select("select * from tb_dbinfo limit #{skipcount},#{pagesize}")
    @Results({
            @Result(property ="driverUrl" ,column ="driver_url" ),
            @Result(property = "user", column = "db_user"),
            @Result(property = "password", column = "db_password"),
            @Result(property = "driverName",column = "driver_name"),
            @Result(property = "desc",column = "db_desc")
    })
    List<DBInfo>  queryAll(@Param("skipcount") int skipcount, @Param("pagesize") int pagesize);
    @Select("select count(*) from tb_dbinfo where name=#{name}")
    int queryName(@Param("name") String name);

    @Select("select * from tb_dbinfo where name=#{name}")
    @Results({
            @Result(property ="driverUrl" ,column ="driver_url" ),
            @Result(property = "user", column = "db_user"),
            @Result(property = "password", column = "db_password"),
            @Result(property = "driverName",column = "driver_name"),
            @Result(property = "desc",column = "db_desc")
    })
    DBInfo queryByName(@Param("name") String name);

    @Delete("delete from tb_dbinfo where name=#{name}")
    int deleteByName(@Param("name") String name);

    @Select("select count(*) from tb_dbinfo")
    int selectCount();

    @Select("select name from tb_dbinfo")
    List<DBInfo> queryAllName();
}
